

class Main extends egret.DisplayObjectContainer {

    /**
     * 加载进度界面
     * Process interface loading
     */
    private loadingView: LoadingUI;

    public constructor() {
        super();
        this.addEventListener(egret.Event.ADDED_TO_STAGE, this.onAddToStage, this);
    }

    private onAddToStage(event: egret.Event) {
        //设置加载进度界面
        //Config to load process interface
        this.loadingView = new LoadingUI();
        this.stage.addChild(this.loadingView);


        //初始化Resource资源加载库
        //initiate Resource loading library
        RES.addEventListener(RES.ResourceEvent.CONFIG_COMPLETE, this.onConfigComplete, this);
        RES.loadConfig("resource/default.res.json", "resource/");
    }

    /**
     * 配置文件加载完成,开始预加载preload资源组。
     * configuration file loading is completed, start to pre-load the preload resource group
     */
    private onConfigComplete(event: RES.ResourceEvent): void {
        RES.removeEventListener(RES.ResourceEvent.CONFIG_COMPLETE, this.onConfigComplete, this);
        RES.addEventListener(RES.ResourceEvent.GROUP_COMPLETE, this.onResourceLoadComplete, this);
        RES.addEventListener(RES.ResourceEvent.GROUP_LOAD_ERROR, this.onResourceLoadError, this);
        RES.addEventListener(RES.ResourceEvent.GROUP_PROGRESS, this.onResourceProgress, this);
        RES.addEventListener(RES.ResourceEvent.ITEM_LOAD_ERROR, this.onItemLoadError, this);
        RES.loadGroup("preload");
    }

    /**
     * preload资源组加载完成
     * Preload resource group is loaded
     */
    private onResourceLoadComplete(event: RES.ResourceEvent) {
        if (event.groupName == "preload") {
            this.stage.removeChild(this.loadingView);
            RES.removeEventListener(RES.ResourceEvent.GROUP_COMPLETE, this.onResourceLoadComplete, this);
            RES.removeEventListener(RES.ResourceEvent.GROUP_LOAD_ERROR, this.onResourceLoadError, this);
            RES.removeEventListener(RES.ResourceEvent.GROUP_PROGRESS, this.onResourceProgress, this);
            RES.removeEventListener(RES.ResourceEvent.ITEM_LOAD_ERROR, this.onItemLoadError, this);
            this.createGameScene();
        }
    }

    /**
     * 资源组加载出错
     *  The resource group loading failed
     */
    private onItemLoadError(event: RES.ResourceEvent) { console.warn("Url:" + event.resItem.url + " has failed to load"); }

    /**
     * 资源组加载出错
     *  The resource group loading failed
     */
    private onResourceLoadError(event: RES.ResourceEvent) {
        //TODO
        console.warn("Group:" + event.groupName + " has failed to load");
        //忽略加载失败的项目
        //Ignore the loading failed projects
        this.onResourceLoadComplete(event);
    }

    /**
     * preload资源组加载进度
     * Loading process of preload resource group
     */
    private onResourceProgress(event: RES.ResourceEvent) { if (event.groupName == "preload") { this.loadingView.setProgress(event.itemsLoaded, event.itemsTotal); } }
    private textfield: egret.TextField;

    /**
     * 创建游戏场景
     * Create a game scene
     */
    private createGameScene(): void {
        //首页
        // var HomePage = new HomePageUI(); 
        // this.$stage.addChild(HomePage);
        // 二次进入页面
        var secondeUI = new SecondUI();
        this.$stage.addChild(secondeUI);
        //尝试游戏场景跳转
        // var senceManage = new SenceManage(this.stage);
        // var senceManage = SenceManage.create(this.stage);
        // senceManage.loadSence("ResultUI",null,ResultUI,function(){alert("加载完成")})

        //初始化游戏界面,开始游戏!
        // var gameLayer = new GameUI(); 
        // this.$stage.addChild(gameLayer);

        // var secondUI= new SecondUI();
        // this.$stage.addChild(secondUI);


        //音乐图标
        // var music_logo = createBitmap("music_logo_png", 80, 50);
        var music_logo = createBitmap("music_logo_png", 32.5, 32.5);
        this.addChild(music_logo);
        music_logo.anchorOffsetX = 65 * .5;
        music_logo.anchorOffsetY = 65 * .5;
        var tw = egret.Tween.get(music_logo, { loop: true }).to({ rotation: 360 }, 2000);
        music_logo.touchEnabled = true;
        //声音加载
        var sound = new egret.Sound;
        var soundChannel;
        var music_Length = 0;
        var curTime = 0;
        sound.addEventListener(egret.Event.COMPLETE, function () {
            soundChannel = sound.play(curTime, 0);
            music_Length = sound.length;
        }, this);
        sound.load("resource/assets/music.mp3");
        //实现点击music_logo图标播放/暂停功能
        music_logo.addEventListener(egret.TouchEvent.TOUCH_TAP, function () {
            if (soundChannel === null) {
                soundChannel = sound.play(curTime);
                soundChannel.addEventListener(egret.TouchEvent.TOUCH_TAP, function () {
                })
                egret.Tween.resumeTweens(music_logo);
            } else {
                curTime = soundChannel.position;
                soundChannel.stop();
                soundChannel = null;
                egret.Tween.pauseTweens(music_logo);
            }
        }, this);


    }

    /**
     * 根据name关键字创建一个Bitmap对象。name属性请参考resources/resource.json配置文件的内容。
     * Create a Bitmap object according to name keyword.As for the property of name please refer to the configuration file of resources/resource.json.
     */
    private createBitmapByName(name: string) {
        let result = new egret.Bitmap();
        let texture: egret.Texture = RES.getRes(name);
        result.texture = texture;
        return result;
    }

    /**
     * 描述文件加载成功，开始播放动画
     * Description file loading is successful, start to play the animation
     */
    private startAnimation(result: string[]) {
        let parser = new egret.HtmlTextParser();

        let textflowArr = result.map(text => parser.parse(text));
        let textfield = this.textfield;
        let count = -1;
        let change = () => {
            count++;
            if (count >= textflowArr.length) {
                count = 0;
            }
            let textFlow = textflowArr[count];

            // 切换描述内容
            // Switch to described content
            textfield.textFlow = textFlow;
            let tw = egret.Tween.get(textfield);
            tw.to({ "alpha": 1 }, 200);
            tw.wait(2000);
            tw.to({ "alpha": 0 }, 200);
            tw.call(change, this);
        };
        change();
    }


}

